package com.company.cloud.web.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import org.springframework.util.StringUtils;

import java.util.*;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class WebArtUtils {

    public static JSONObject getArtistInfo(String artistNo) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("artistNo", artistNo);
        String body = HttpUtil.get(WebUrlUtils.getUrl(WebUrlUtils.Artist_Get_Url), paramMap);
        JSONObject artist = JSON.parseObject(body);
        return artist;
    }

    public static JSONObject listArtistInfoPy(String nameFirstPy) {
        Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(nameFirstPy)) {
            paramMap.put("nameFirstPy", nameFirstPy.toLowerCase());
        }
        String body = HttpRequest.post(WebUrlUtils.getUrl(WebUrlUtils.Artist_PY_Query_Url))
                .body(JSON.toJSONString(paramMap)).execute().body();
        JSONObject data = JSON.parseObject(body);
        return data;
    }

    public static JSONObject queryArtistInfo(Map<String, String> params) {
        Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(params.get("page"))) {
            paramMap.put("page", Integer.parseInt(params.get("page")));
        }
        if (StrUtil.isNotEmpty(params.get("rows"))) {
            paramMap.put("rows", Integer.parseInt(params.get("rows")));
        }

        if (StrUtil.isNotEmpty(params.get("artistNo"))) {
            paramMap.put("artistNo", params.get("artistNo"));
        }
        if (StrUtil.isNotEmpty(params.get("name"))) {
            paramMap.put("name", params.get("name"));
        }

        if (StrUtil.isNotEmpty(params.get("companyNo"))) {
            paramMap.put("companyNo", params.get("companyNo"));
        }

        String body = HttpRequest.post(WebUrlUtils.getUrl(WebUrlUtils.Artist_Query_Url))
                .body(JSON.toJSONString(paramMap)).execute().body();
        JSONObject data = JSON.parseObject(body);
        return data;
    }

    public static JSONObject queryOrganInfo(Map<String, String> params) {
        Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(params.get("page"))) {
            paramMap.put("page", Integer.parseInt(params.get("page")));
        }
        if (StrUtil.isNotEmpty(params.get("rows"))) {
            paramMap.put("rows", Integer.parseInt(params.get("rows")));
        }
        if (StrUtil.isNotEmpty(params.get("companyNo"))) {
            paramMap.put("companyNo", params.get("companyNo"));
        }
        if (StrUtil.isNotEmpty(params.get("name"))) {
            paramMap.put("name", params.get("name"));
        }
        if (StrUtil.isNotEmpty(params.get("type"))) {
            paramMap.put("type", params.get("type"));
        }
        String body = HttpRequest.post(WebUrlUtils.getUrl(WebUrlUtils.Organ_Query_Url))
                .body(JSON.toJSONString(paramMap)).execute().body();
        JSONObject data = JSON.parseObject(body);
        return data;
    }

    public static JSONObject getOrganInfo(String organNo) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", organNo);
        String body = HttpRequest.post(WebUrlUtils.getUrl(WebUrlUtils.Organ_Get_Url))
                .body(JSON.toJSONString(paramMap)).execute().body();
        JSONObject organ = JSON.parseObject(body);
        return organ;

    }

    public static JSONObject getNewsInfo(String companyArticleNo) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("companyArticleNo", companyArticleNo);
        String body = HttpUtil.get(WebUrlUtils.getUrl(WebUrlUtils.News_Get_Url), paramMap);
        JSONObject data = JSON.parseObject(body);
        return data;
    }


    public static JSONObject queryNewsInfo(Map<String, String> params) {
        Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(params.get("page"))) {
            paramMap.put("page", Integer.parseInt(params.get("page")));
        }
        if (StrUtil.isNotEmpty(params.get("rows"))) {
            paramMap.put("rows", Integer.parseInt(params.get("rows")));
        }

        if (StrUtil.isNotEmpty(params.get("articleRelNo"))) {
            paramMap.put("articleRelNo", params.get("articleRelNo"));
        }

        if (StrUtil.isNotEmpty(params.get("title"))) {
            paramMap.put("title", params.get("title"));
        }

        if (StrUtil.isNotEmpty(params.get("tag"))) {
            paramMap.put("tag", params.get("tag"));
        }

        String body = HttpRequest
                .post(WebUrlUtils.getUrl(WebUrlUtils.News_Query_Url))
                .header("tenant", "PTC")
                .body(JSON.toJSONString(paramMap)).execute().body();
        JSONObject data = JSON.parseObject(body);
        return data;
    }


    public static JSONObject getTag(String[] tagIds) {
        Map<String, Object> paramMap = new HashMap<>();
        if (tagIds != null && tagIds.length > 0) {
            Set<String> ids = new HashSet<>();
            for (String tagId : tagIds) {
                ids.add(tagId);
            }
            paramMap.put("typeNos", ids);
        }
        String body = HttpRequest.post(WebUrlUtils.getUrl(WebUrlUtils.Tag2Type_Get_Url))
                .body(JSON.toJSONString(paramMap)).execute().body();
        JSONObject data = JSON.parseObject(body);
        return data;
    }

    public static Object getParameter(String key) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("key", key);
        String body = HttpUtil.get(WebUrlUtils.getUrl(WebUrlUtils.Parameter_Get_Url), paramMap);

        if (StringUtils.isEmpty(body)) {
            return null;
        }
        JSONObject data = JSON.parseObject(body);
        String val = data.getString("data");
        if (StrUtil.isEmpty(val)) {
            return null;
        }
        String indexStr = val.substring(0, 1);
        if ("[".equals(indexStr)) {
            return JSON.parseArray(val);
        }
        if ("{".equals(indexStr)) {
            return JSON.parseObject(val);
        }
        return null;
    }

    public static JSONObject expoQuery(Map<String, String> params) {
        Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(params.get("page"))) {
            paramMap.put("page", Integer.parseInt(params.get("page")));
        }
        if (StrUtil.isNotEmpty(params.get("rows"))) {
            paramMap.put("rows", Integer.parseInt(params.get("rows")));
        }
        if (StrUtil.isNotEmpty(params.get("name"))) {
            paramMap.put("name", params.get("name"));
        }
        if (StrUtil.isNotEmpty(params.get("bizVal1"))) {
            paramMap.put("bizVal1", params.get("bizVal1"));
        }
        String body = HttpRequest.post(WebUrlUtils.getUrl(WebUrlUtils.Expo_Query))
                .body(JSON.toJSONString(paramMap)).execute().body();
        if (StrUtil.isNotEmpty(body)) {
            JSONObject data = JSON.parseObject(body);
            return data;
        }
        return null;
    }

    public static JSONObject expoInfo(String expoNo) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", expoNo);
        String body = HttpUtil.get(WebUrlUtils.getUrl(WebUrlUtils.Expo_Get), paramMap);
        if (StrUtil.isNotEmpty(body)) {
            JSONObject data = JSON.parseObject(body);
            return data;
        }
        return null;
    }

    public static JSONObject expoJoinOrgQuery(Map<String, String> params) {
        Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(params.get("page"))) {
            paramMap.put("page", Integer.parseInt(params.get("page")));
        }
        if (StrUtil.isNotEmpty(params.get("rows"))) {
            paramMap.put("rows", Integer.parseInt(params.get("rows")));
        }

        if (StrUtil.isNotEmpty(params.get("expoNo"))) {
            paramMap.put("expoNo", params.get("expoNo"));
        }
        if (StrUtil.isNotEmpty(params.get("orgNo"))) {
            paramMap.put("orgNo", params.get("orgNo"));
        }
        if (StrUtil.isNotEmpty(params.get("boothNo"))) {
            paramMap.put("boothNo", params.get("boothNo"));
        }
        if (StrUtil.isNotEmpty(params.get("state"))) {
            paramMap.put("state", params.get("state"));
        }
        if (StrUtil.isNotEmpty(params.get("nameFirstPy"))) {
            paramMap.put("nameFirstPy", params.get("nameFirstPy"));
        }

        String body = HttpRequest.post(WebUrlUtils.getUrl(WebUrlUtils.Expo_Join_Query))
                .body(JSON.toJSONString(paramMap)).execute().body();

        if (StrUtil.isNotEmpty(body)) {
            JSONObject data = JSON.parseObject(body);
            return data;
        }
        return null;
    }

    public static JSONObject staticPageTree(String type) {
        Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(type)) {
            paramMap.put("type", type);
        }
        String body = HttpRequest.post(WebUrlUtils.getUrl(WebUrlUtils.Page_Static_Tree_Url))
                .body(JSON.toJSONString(paramMap)).execute().body();
        if (StrUtil.isNotEmpty(body)) {
            JSONObject data = JSON.parseObject(body);
            return data;
        }
        return null;
    }

    @SneakyThrows
    public static JSONObject CategoryTree(String companyNo) {
    /*    Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(companyNo)) {
            paramMap.put("companyNo", companyNo);
        }
        String body = HttpUtil.get(WebUrlUtils.getUrl(WebUrlUtils.News_Category_Url), paramMap);
        if (StrUtil.isNotEmpty(body)) {
            JSONObject data = JSON.parseObject(body);
            return data;
        }*/
        // 获取连接客户端工具
        CloseableHttpClient httpClient = HttpClients.createDefault();

        String entityStr = null;
        CloseableHttpResponse response = null;

            /*
             * 由于GET请求的参数都是拼装在URL地址后方，所以我们要构建一个URL，带参数
             */
            URIBuilder uriBuilder = new URIBuilder(WebUrlUtils.getUrl(WebUrlUtils.News_Category_Url));
            /** 第一种添加参数的形式 */
          /*uriBuilder.addParameter("name", "root");
          uriBuilder.addParameter("password", "123456");*/
            /** 第二种添加参数的形式 */
            List<NameValuePair> list = new LinkedList<>();
            BasicNameValuePair param1 = new BasicNameValuePair("companyNo", companyNo);
            list.add(param1);
            uriBuilder.setParameters(list);

            // 根据带参数的URI对象构建GET请求对象
            HttpGet httpGet = new HttpGet(uriBuilder.build());

            /*
             * 添加请求头信息
             */
            // 浏览器表示
            httpGet.addHeader("tenant", "PTC");
            httpGet.addHeader("token", "AUK_1460452592277655552");
            // 执行请求
            response = httpClient.execute(httpGet);
            // 获得响应的实体对象
            HttpEntity entity = response.getEntity();
            // 使用Apache提供的工具类进行转换成字符串
            entityStr= EntityUtils.toString(response.getEntity());
        System.out.println(JSON.parseObject(entityStr));
        if (StrUtil.isNotEmpty(entityStr)) {
            JSONObject data = JSON.parseObject(entityStr);
            return data;
        }
        return null;
    }
    public static JSONObject getStaticPage(String pageNo) {
        Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(pageNo)) {
            paramMap.put("pageNo", pageNo);
        }
        String body = HttpUtil.get(WebUrlUtils.getUrl(WebUrlUtils.Page_Static_Info_Url), paramMap);
        if (StrUtil.isNotEmpty(body)) {
            JSONObject data = JSON.parseObject(body);
            return data;
        }
        return null;
    }


    public static JSONObject expoWorksQuery(String expoNo) {
        Map<String, Object> paramMap = new HashMap<>();
        if (StrUtil.isNotEmpty(expoNo)) {
            paramMap.put("expoNo", expoNo);
        }
        String body = HttpRequest.post(WebUrlUtils.getUrl(WebUrlUtils.Expo_Join_Product_Url))
                .body(JSON.toJSONString(paramMap)).execute().body();
        if (StrUtil.isNotEmpty(body)) {
            JSONObject data = JSON.parseObject(body);
            return data;
        }
        return null;
    }


//    public static List<SitMapModel> getNewsSitMap(){
//        return  null;
//    }
//
//    public static List<SitMapModel> getArtistSitMap(){
//        return  null;
//    }
//
//    public static List<SitMapModel> getOrganSitMap(){
//        return  null;
//    }

}
